package com.example.myapp;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.math.BigDecimal;

public class CalculateActivity extends AppCompatActivity {
    private static final String TAG ="CalculateActivity";

    String id,age,weight,height,sex,type,str;
   double bmi,bmr,sta;
    TextView BMI,BMR,Sug;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculate);

        BMI = findViewById(R.id.BMI);
        BMR = findViewById(R.id.BMR);
        Sug = findViewById(R.id.Sug);
        //调用方法
        getAndsetIntentData();


    }


    void getAndsetIntentData(){
        if(getIntent().hasExtra("id1")&&
                getIntent().hasExtra("age1")&&
                getIntent().hasExtra("height1")&&
                getIntent().hasExtra("weight1")){
            //从intent中获取数据
            id = getIntent().getStringExtra("id1");
            age = getIntent().getStringExtra("age1");
            height = getIntent().getStringExtra("height1");
            weight = getIntent().getStringExtra("weight1");
            sex = getIntent().getStringExtra("sex1");
            type = getIntent().getStringExtra("type1");
            bmi= Double.valueOf(weight)/(Double.valueOf(height)*0.01*Double.valueOf(height)*0.01);            BMI.setText(String.valueOf(bmi));
          double show_bmi;
           BigDecimal f = new BigDecimal(bmi);
            //保留2位小数
            show_bmi = f.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            BMI.setText(String.valueOf(show_bmi));
            Log.i(TAG,"onCrate:"+bmi);

            if (bmi<=18.4){ str = "您的身体状态：偏瘦"; }
            else if (bmi<=23.9){ str = "您的身体状态：正常"; }
            else if(bmi<=27.9) { str = "您的身体状态：偏重"; }
            else { str = "您的身体状态：肥胖";
            }
            Log.i(TAG,"onCrate:"+str);

            if (type.equals("坐式生活方式（极少运动）")){
                sta = 1.15; }
            else if (type.equals("低强度健身（每周运动1-2次）")){
                sta = 1.3; }
            else if (type.equals("中强度健身（每周运动3-5次）")){
                sta = 1.4; }
            else { sta = 1.6; }
            Log.i(TAG,"onCrate:"+sta);

            if(sex.equals("男")){
                bmr=(67+13.73*Double.valueOf(weight)+5*Double.valueOf(height)-6.9*Double.valueOf(age))*sta;
                double a = 0.0;
                BigDecimal d = new BigDecimal(bmr);
                //保留2位小数
                a = d.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
                BMR.setText(String.valueOf(a));

            }
            else if(sex.equals("女")) {
                bmr=(661+9.6*Double.valueOf(weight)+1.72*Double.valueOf(height)*0.01-4.7*Double.valueOf(age))*sta;
                double a = 0.0;
                BigDecimal e = new BigDecimal(bmr);
                //保留2位小数
                a = e.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
                BMR.setText(String.valueOf(a));

            }
            Log.i(TAG,"onCrate:"+bmr);

            //将从intent处获得的数据显示在页面上
            Sug.setText(str);
        }
        else {
            Toast.makeText(this,"没有数据",Toast.LENGTH_SHORT).show();
        }
    }
}
